package com.yanqu.road.server.manger;

import com.yanqu.road.entity.servercenter.CrossServerGroup;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.logic.bussiness.servercenter.ServerBussiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manger.plugin.ManagerReloadListener;
import org.apache.bcel.verifier.statics.LONG_Upper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossServerGroupMgr extends TempMgr {

    private static final Logger logger = LogManager.getLogger(CrossServerGroupMgr.class.getName());

    private static Map<Integer, Map<Integer, CrossServerGroup>> channelServerGroupMap;

    private static Map<Integer,CrossServerGroup> serverGroupMap;

    private static ManagerReloadListener managerReloadListener;

    @Override
    public boolean reload() throws Exception {
        reloadServerGroup();
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static void reloadServerGroup(){
        Map<Integer, Map<Integer, CrossServerGroup>> tempChannelServerGroupMap = new ConcurrentHashMap<>();
        Map<Integer, CrossServerGroup> tempServerGroupMap = new ConcurrentHashMap<>();
        ServerBussiness.getCrossServerGroupMap(tempChannelServerGroupMap, tempServerGroupMap);
        channelServerGroupMap = tempChannelServerGroupMap;
        serverGroupMap = tempServerGroupMap;
        notifyReload();
    }

    private static void notifyReload(){
        if(managerReloadListener != null){
            managerReloadListener.notifyReload();
        }
    }

    public static void addReloadListener(ManagerReloadListener reloadListener){
        managerReloadListener = reloadListener;
    }

    public static Map<Integer, CrossServerGroup> getServerGroupMap(int channelId){
        return channelServerGroupMap.get(channelId);
    }

    public static List<CrossServerGroup> getServerGroupList(int channelId){
        Map<Integer, CrossServerGroup> dataMap = getServerGroupMap(channelId);
        if(null != dataMap){
            return new ArrayList<>(dataMap.values());
        }
        return new ArrayList<>();
    }

    public static CrossServerGroup getServerGroup(int groupId){
        return serverGroupMap.get(groupId);
    }

    public static int getGroupIdByServerId(long serverId){
        int groupId = (int)serverId;
        CrossServerGroup serverGroup = getServerGroupByServerId(serverId);
        if(null != serverGroup && serverGroup.getCrossZsState() == 1){
            groupId = serverGroup.getGroupId();
        }
        return groupId;
    }

    public static CrossServerGroup getServerGroupByServerId(long serverId){
        CrossServerGroup serverGroup = null;
        ServerInfo serverInfo = ServerListMgr.getServerInfo(serverId);
        if(null == serverInfo){
            for(CrossServerGroup tempGroup : serverGroupMap.values()){
                if(tempGroup.getServerList().contains(serverId)){
                    serverGroup = tempGroup;
                    break;
                }
            }
        }else {
            Map<Integer, CrossServerGroup> dataMap = getServerGroupMap(serverInfo.getChannelId());
            if(null != dataMap){
                for(CrossServerGroup tempGroup : dataMap.values()){
                    if(tempGroup.getServerList().contains(serverId)){
                        serverGroup = tempGroup;
                        break;
                    }
                }
            }
        }
        return serverGroup;
    }

}
